<?php

/**
 * 当对象间存在一对多关系时，则使用观察者模式（Observer Pattern）。比如，当一个对象被修改时，则会自动通知依赖它的对象。观察者模式属于行为型模式。
 */
class CreateOrderSubject{
    private $events;
    public function AddEvent(Event $event){
        $this->events[] = $event;
    }
    public function handle(){
        foreach ($this->events as $e){
            $e->handle();
        }
    }
}
abstract class Event{
    protected $data;
    function __construct($data){
        $this->data = $data;
    }
   abstract public function handle();
}

class WuLiuEvent extends Event{
    function handle(){
        echo "新增第三方物流订单：".$this->data["OrderId3rd"]."\n";
    }
}
class KuCunEvent extends Event{
    function handle(){
        echo "扣减库存：".$this->data["num"]."\n";
    }
}
function main(){

}
$createOrder = new CreateOrderSubject();
$wuLiuData["OrderId3rd"] ="15212343123";
$kuCunData["num"] = 1;
$createOrder->AddEvent(new WuLiuEvent($wuLiuData));
$createOrder->AddEvent(new KuCunEvent($kuCunData));

$createOrder->handle();